<template>
  <div class="filters">
    <el-row type="flex" class="filters-top" justify="space-between" align="middle">
      <el-col :span="8">
        单程：
        {{headData.info.departCity}} - {{headData.info.destCity}}
        /
        {{headData.info.departDate}}
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airport" placeholder="起飞机场" @change="RunCommonFilter">
          <el-option
            v-for="(item,index) in headData.options.airport"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="flightTimes" placeholder="起飞时间" @change="RunCommonFilter">
          <el-option
            v-for="(item,index) in headData.options.flightTimes"
            :key="index"
            :label="`${item.from}:00 - ${item.to}:00`"
            :value="`${item.from},${item.to}`"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="company" placeholder="航空公司" @change="RunCommonFilter">
          <el-option
            v-for="(item,index) in headData.options.company"
            :key="index"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select size="mini" v-model="airSize" placeholder="机型" @change="RunCommonFilter">
          <el-option
            v-for="(item,index) in sizeOptions"
            :key="index"
            :label="item.name"
            :value="item.size"
          ></el-option>
        </el-select>
      </el-col>
    </el-row>
    <div class="filter-cancel">
      筛选：
      <el-button type="primary" round plain size="mini" @click="handleFiltersCancel">撤销</el-button>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    headData: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  mounted() {
    console.log("接收过来的数据", this.headData);
  },
  data() {
    return {
      airport: "", // 机场
      flightTimes: "", // 出发时间
      company: "", // 航空公司
      airSize: "", // 机型大小
      sizeOptions: [
        { name: "大", size: "L" },
        { name: "中", size: "M" },
        { name: "小", size: "S" }
      ]
    };
  },
  methods: {
    // 公共筛选函数
    RunCommonFilter() {
      let newFlightsdata = this.headData.flights;
      if (this.airport) {
        newFlightsdata = this.handleAirport(newFlightsdata);
      }
      if (this.flightTimes) {
        newFlightsdata = this.handleFlightTimes(newFlightsdata);
      }
      if (this.company) {
        newFlightsdata = this.handleCompany(newFlightsdata);
      }
      if (this.airSize) {
        newFlightsdata = this.handleAirSize(newFlightsdata);
      }
      this.$emit("setDataList", newFlightsdata);
    },
    // 选择机场时候触发
    handleAirport(list) {
      const newList = list.filter(element => {
        return element.org_airport_name === this.airport;
      });
      return newList;
    },

    // 选择出发时间时候触发
    handleFlightTimes(list) {
      const [from, to] = this.flightTimes.split(",");
      const newList = list.filter(element => {
        // 出发时间小时
        const start = +element.dep_time.split(":")[0];
        return start >= from && start < to;
      });
      return newList;
    },

    // 选择航空公司时候触发
    handleCompany(list) {
      const newList = list.filter(element => {
        return element.airline_name === this.company;
      });
      return newList;
    },

    // 选择机型时候触发
    handleAirSize(list) {
      const newList = list.filter(element => {
        return element.plane_size === this.airSize;
      });
      return newList;
    },

    // 撤销条件时候触发
    handleFiltersCancel() {
      // 撤销就全部清空，恢复到初始的情况
      this.airport = "";
      this.flightTimes = "";
      this.company = "";
      this.airSize = "";
      // this.$emit("setDataList", this.headData.flights);
      // 第二种写法  执行过滤函数
      this.RunCommonFilter();
    }
  }
};
</script>

<style scoped lang="less">
.filters {
  margin-bottom: 20px;
}

.filters-top {
  > div {
    /deep/ .el-select {
      margin-left: 10px;
    }
  }
}

.filter-cancel {
  margin-top: 10px;
}
</style>
